import { defineStore } from 'pinia'
import { ref, computed } from 'vue'

export const useUserStore = defineStore('user', () => {
    // 状态 (state)
    const token = ref('')
    const userInfo = ref<{ name: string; age: number } | null>(null)

    // 计算属性 (getters)
    const isLogin = computed(() => !!token.value)
    const userName = computed(() => userInfo.value?.name || '未登录')

    // 方法 (actions)
    const login = (name: string, age: number) => {
        token.value = 'mock-token'
        userInfo.value = { name, age }
    }

    const logout = () => {
        token.value = ''
        userInfo.value = null
    }

    return {
        token,
        userInfo,
        isLogin,
        userName,
        login,
        logout
    }
})